package cn.gzten.lambda.dynamo.sample.data;

import cn.gzten.annotation.RegisterReflection;
import cn.gzten.lambda.dynamo.annotation.DynamoBean;
import cn.gzten.lambda.dynamo.annotation.PartitionKey;
import com.fasterxml.jackson.annotation.*;
import lombok.Data;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Data
@DynamoBean(tableName = "gzten.user")
public class AppUser {
    private static final DateTimeFormatter DT_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

    @PartitionKey
    private String username;

    private String password;

    @JsonIgnore
    private Long lastUpdateTime;

    @JsonIgnore
    private Long creationTime;

    @JsonAlias("lastUpdateDateTime")
    public String getLastUpdateDateTime() {
        if (lastUpdateTime == null) {
            return "";
        }
        LocalDateTime dateTime = Instant.ofEpochMilli(lastUpdateTime)
                .atOffset(ZoneOffset.UTC)
                .toZonedDateTime().toLocalDateTime();
        return dateTime.format(DT_FMT);
    }

    @JsonAlias("creationDateTime")
    public String getCreationDateTime() {
        if (creationTime == null) {
            return "";
        }
        LocalDateTime dateTime = Instant.ofEpochMilli(creationTime)
                .atOffset(ZoneOffset.UTC)
                .toZonedDateTime().toLocalDateTime();
        return dateTime.format(DT_FMT);
    }

}

